//
//  BVIndexDetailsViewController.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/6.
//  Copyright © 2017年 YYKJ. All rights reserved.
//  首页推荐详情页

#import "BVIndexDetailsViewController.h"
#import "BVIndexViewController.h"
#import "UIButton+BVButtonAnimation.h"
#import "BVIndexDetailsCell.h"
#import "BVIndexDetailsCell2.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "BVIndexDetailsCell3.h"
#import "BVMyPhotoAlertView.h"

#define BUY_TABViEW_CELL1 @ "BVIndexDetailsCell"
#define BUY_TABViEW_CELL2 @ "BVIndexDetailsCell2"
#define BUY_TABViEW_CELL3 @ "BVIndexDetailsCell3"

#define pictureHeight 200
@interface BVIndexDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>

@property (nonatomic ,strong) UITableView *tabView;
@property (nonatomic ,strong) UIView *navView;
@property (nonatomic ,strong) NSArray *titleArr;
@property (nonatomic ,strong) UIImageView *headerView;
@property (nonatomic ,strong) UIImageView *pictureImageView;
@property (nonatomic ,strong) UIView *header;
@property (nonatomic ,strong) UIImageView *avatarImage;

@end

@implementation BVIndexDetailsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor BVBackgroundLightGrayColor]];
   
    [self layoutUI];
    [self setBottomView];
    
}

-(void)layoutUI{
    
    [self.view addSubview:self.tabView];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    NSString *str = @"d声明梦会计师考虑刚万别看过你去乌克兰给你看了人情味了虑刚万别看过虑刚万别看过虑刚万别看过";
    if (indexPath.section == 0) {
        
        return [self.tabView cellHeightForIndexPath:indexPath model:str keyPath:@"text" cellClass:[BVIndexDetailsCell class] contentViewWidth:ScreenW];
        
    }else if (indexPath.section == 1){
        return 100;
    }else{
        return 50;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArr.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *arr = self.titleArr[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        BVIndexDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:BUY_TABViEW_CELL1];
        [cell.titleLb setText:[NSString stringWithFormat:@"%@",self.titleArr[indexPath.section][indexPath.row][@"titel"]]];
        cell.text = @"d声明梦会计师考虑刚万别看过你去乌克兰给你看了人情味了虑刚万别看过虑刚万别看过虑刚万别看过";
        return cell;
    }if (indexPath.section == 1) {
        BVIndexDetailsCell3 *cell = [tableView dequeueReusableCellWithIdentifier:BUY_TABViEW_CELL3];
        cell.amplificationBlock = ^(NSInteger tag) {
            NSArray *imgUrls = @[@"http://pic.58pic.com/58pic/13/12/24/21E58PICfx8_1024.jpg",@"http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1604/18/c19/20473373_1460987030407_mthumb.jpg",@"http://cdnq.duitang.com/uploads/item/201507/04/20150704223530_zWVC5.jpeg"];
            BVMyPhotoAlertView *picView = [[BVMyPhotoAlertView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) withImgs:nil withImgUrl:imgUrls withOfset:1];

            [picView showMKPAlertView];
        };
        return cell;
    }else{
        BVIndexDetailsCell2 *cell = [tableView dequeueReusableCellWithIdentifier:BUY_TABViEW_CELL2];
        [cell.titleLb setText:[NSString stringWithFormat:@"%@",self.titleArr[indexPath.section][indexPath.row][@"titel"]]];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}

- (void)setBottomView{
    
    UIView *bottmView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenH-40, ScreenW, ScreenH-(ScreenH-40))];
    [self.view addSubview:bottmView];
    [bottmView setBackgroundColor:[UIColor whiteColor]];
    
    UIView *line = [[UIView alloc]init];
    [bottmView addSubview:line];
    [line setBackgroundColor:[UIColor BVLineGrayColor]];
    [line setFrame:CGRectMake(0, 0, ScreenW, 1.0)];
    
    UIButton *btn1 = [[UIButton alloc]init];
    [bottmView addSubview:btn1];
    [btn1 setTitle:@"请红娘前线" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor BVTextDarkGrayColor] forState:UIControlStateNormal];
    [btn1.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [btn1 setImage:[UIImage imageNamed:@"weituo"] forState:UIControlStateNormal];
    btn1.sd_layout
    .leftSpaceToView(bottmView, 10)
    .topSpaceToView(bottmView, 1)
    .widthIs(120)
    .heightIs(39);
    
    UIButton *btn2 = [[UIButton alloc]init];
    [bottmView addSubview:btn2];
    [btn2 setTitle:@"发消息" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor BVTextDarkGrayColor] forState:UIControlStateNormal];
    [btn2.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [btn2 setImage:[UIImage imageNamed:@"zhaohu"] forState:UIControlStateNormal];
    btn2.sd_layout
    .leftSpaceToView(btn1, 20)
    .topSpaceToView(bottmView, 1)
    .widthIs(80)
    .heightIs(39);
    
    UIButton *btn3 = [[UIButton alloc]init];
    [bottmView addSubview:btn3];
    [btn3 setTitle:@"有好感" forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor BVTextDarkGrayColor] forState:UIControlStateNormal];
    [btn3.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [btn3 setImage:[UIImage imageNamed:@"discollection"] forState:UIControlStateNormal];
    [btn3 setImage:[UIImage imageNamed:@"collection"] forState:UIControlStateSelected];
    [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn3.sd_layout
    .topSpaceToView(bottmView, 1)
    .rightSpaceToView(bottmView, 10)
    .widthIs(80)
    .heightIs(39);
    
}

-(void)btnClick:(UIButton *)btn{
    
    if (btn.selected == YES) {
        btn.selected = NO;
    }else{
        btn.selected = YES;
    }
}

#pragma mark - 导航条渐变
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setDelegate:self];
    self.navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 64)];
    [self.view addSubview:self.navView];
    [self.navView setBackgroundColor:[UIColor BVNavBackgroundColor]];
    
    UIButton *leftBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.navView addSubview:leftBtn];
    [leftBtn addTarget:self action:@selector(navleftBackBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setFrame:CGRectMake(0, 22, 100, 44)];
    [leftBtn setImage:[UIImage imageNamed:@"back_3"] forState:UIControlStateNormal];
    [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 10, 70)];
    
    UIButton *rightBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.navView addSubview:rightBtn];
    [rightBtn setFrame:CGRectMake(ScreenW-10-30, 26, 30, 28)];
    [rightBtn addTarget:self action:@selector(navRightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setImage:[UIImage imageNamed:@"haogan"] forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"haoganok"] forState:UIControlStateSelected];
    
    UILabel *navTitleLb = [UILabel new];
    [self.navView addSubview:navTitleLb];
    [navTitleLb setText:@"王小花"];
    [navTitleLb setTextColor:COLOR_RGB(254, 255, 254, 1.0)];
    [navTitleLb setFont:[UIFont systemFontOfSize:16]];
    [navTitleLb setTextAlignment:NSTextAlignmentCenter];
    navTitleLb.sd_layout
    .topSpaceToView(self.navView, 22)
    .bottomSpaceToView(self.navView, 0)
    .leftSpaceToView(self.navView, 0)
    .rightSpaceToView(self.navView, 0);
    
}

-(void)navleftBackBtnClick{
    
    ROOTNAVPOP;
}

-(void)navRightBtnClick:(UIButton *)sender{
    [UIButton buttonClickAnimation:sender];
    
    if (sender.selected == YES) {
        [self showMessage:@"取消收藏"];
        [sender setSelected:NO];
    }else{
        [self showMessage:@"收藏成功"];
        [sender setSelected:YES];
    }
    
}

#pragma mark - 影藏导航栏  并设置渐变view
- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {
    
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIColor *color=[UIColor BVNavBackgroundColor];
    CGFloat offset=scrollView.contentOffset.y;
    if (offset<0) {
        _navView.backgroundColor = [color colorWithAlphaComponent:0];
    }else {
        CGFloat alpha=1-((64-offset)/64);
        _navView.backgroundColor=[color colorWithAlphaComponent:alpha];
    }
    
    CGFloat Offset_y = scrollView.contentOffset.y;
    if ( Offset_y < 0) {
        CGFloat totalOffset = pictureHeight - Offset_y;
        CGFloat scale = totalOffset / pictureHeight;
        CGFloat width = ScreenW;
        _pictureImageView.frame = CGRectMake(-(width * scale - width) / 2, Offset_y, width * scale, totalOffset);
    }

}

-(UITableView *)tabView{
    if (!_tabView) {
        self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, ScreenW, ScreenH-20) style:UITableViewStylePlain];
        [self.tabView setDelegate:self];
        [self.tabView setDataSource:self];
        [self.tabView setBackgroundColor:[UIColor clearColor]];
        [self.tabView setTableHeaderView:[[UIView alloc]init]];
        [self.tabView setTableHeaderView:[self setTableViewHeaderView]];
        [self.tabView setShowsVerticalScrollIndicator:NO];
        [self.tabView registerClass:[BVIndexDetailsCell class] forCellReuseIdentifier:BUY_TABViEW_CELL1];
        [self.tabView registerClass:[BVIndexDetailsCell2 class] forCellReuseIdentifier:BUY_TABViEW_CELL2];
        [self.tabView registerClass:[BVIndexDetailsCell3 class] forCellReuseIdentifier:BUY_TABViEW_CELL3];
    }
    return _tabView;
}

-(UIView *)setTableViewHeaderView{
    
    _header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, pictureHeight)];

    _pictureImageView = [[UIImageView alloc] initWithFrame:_header.bounds];
    _pictureImageView.image = [UIImage boxblurImage:[UIImage imageNamed:@"information_bac"] withBlurNumber:0.5];
    
    _pictureImageView.contentMode = UIViewContentModeScaleAspectFill;
    _pictureImageView.clipsToBounds = YES;
    [_header addSubview:_pictureImageView];
    
    _avatarImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 90, 90, 90)];
    _avatarImage.image = [UIImage imageNamed:@"999k"];
    [_avatarImage.layer setMasksToBounds:YES];
    [_avatarImage.layer setCornerRadius:5.0];
    [_header addSubview:_avatarImage];
    
    return _header;
}

-(NSArray *)titleArr{
    if (!_titleArr) {
        self.titleArr = [NSArray arrayWithObjects:
                         @[@{@"titel":@"内心独白"}],
                         @[@{@"titel":@" "}],
                         @[@{@"titel":@"*昵称"},
                           @{@"titel":@"*性别"},
                           @{@"titel":@"*身高"},
                           @{@"titel":@"*学历"},
                           @{@"titel":@"*职业"},
                           @{@"titel":@"*月薪"},
                           @{@"titel":@"*所在地"},
                           @{@"titel":@"*出生日期"}],
                         @[@{@"titel":@"择偶-身高"},
                           @{@"titel":@"择偶-年龄"},
                           @{@"titel":@"择偶-学历"},
                           @{@"titel":@"择偶-地址"},
                           @{@"titel":@"择偶-诚信"}],
                         @[@{@"titel":@"公司类型"},
                           @{@"titel":@"工作状态"},
                           @{@"titel":@"锻炼习惯"},
                           @{@"titel":@"是否抽烟"},
                           @{@"titel":@"是否酗酒"},
                           @{@"titel":@"是否和父母同住"}],nil];
    }
    return _titleArr;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
